Repository navigation
Handle errors of the user functions in loo_moment_match() - #410
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #410 +/- ##
==========================================
+ Coverage 92.49% 92.61% +0.11%
==========================================
Files 31 31
Lines 3146 3222 +76
==========================================
+ Hits 2910 2984 +74
- Misses 236 238 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This is how benchmark results would change (along with a 95% confidence interval in relative change) if 035fab8 is merged into master:
|
jgabry
left a comment
There was a problem hiding this comment.
I put a bunch of comments, some from me and some from Codex (that I checked)
… observations fail
…allel test on Windows
|
Thanks, these were all good points. I changed the following.
Tests:
|
jgabry
left a comment
There was a problem hiding this comment.
Thanks, this looks great.
But there's one interaction I didn't anticipate when I suggested the all-fail stop that Codex pointed out:
together with
failed = TRUEwhen the first transformation errors (line 482), it now also stops when every moment-matched observation fails at its first transformation. Often only one to three observations have high k, so a #274-style*_rngoverflow on the first shift now makesloo_moment_match()stop with an error, where master returned the PSIS results. The test at line 811 asserts this case.
I think it's suggestion makes sense:
But the stop is only needed when
log_lik_i()fails everywhere (e.g., wrong arguments). Could it count only the observations wherelog_lik_i()failed (line 291)? Then a first-transformation error would keep the PSIS results with the warning, as in your note on #274.
|
Good catch, changed as you suggested. The stop now counts only the observations where The test at line 811 is split in two. The #274-style case (only observation 1 above the threshold, and its first transformation fails) now checks that |
jgabry
left a comment
There was a problem hiding this comment.
That change looks good. Ready to merge when CI passes.
|
Hmm, actually the benchmark results indicate a ~15% performance regression. Let me look into that before we merge |
Not a problem. The issue is that master got faster! I just merged master into this branch and that should fix it. After the benchmarks run again we can merge. Also this PR doesn't touch anything the benchmarks measure anyway. |
Fixes #274.
This PR was Claude assisted
Summary
loo_moment_match()callslog_prob_upars()andlog_lik_i_upars()on transformed draws, which can be more extreme than the posterior draws, and the model code can throw an error for them. In #274,neg_binomial_2_rngin thegenerated quantitiesblock overflows, because cmdstanr'slog_lik_i_upars()runs the whole block to getlog_lik.The split step at the end called
log_lik_i_upars()withouttry(), so an error there stoppedloo_moment_match(). Withcores > 1and two or more observations,parallel::mclapply()returned the error as a string, and the result loop then failed with "$ operator is invalid for atomic vectors".In the transformation loop,
try()printed the error, and the loop stopped for that observation. The user got no warning that moment matching had stopped early.Change
split = FALSE. The accuracy warning thatsplit = FALSEgives for a large Pareto k of the self-normalized weights now also covers these observations, with a text that refers to the failed split.log_lik_i(), that observation keeps its PSIS results. Atry-errorfrom amclapply()worker gets the same treatment.try()withsilent = TRUE. One warning lists the observations for which moment matching stopped early or failed, and shows the first error message.cmdstanr cannot avoid the error: its
log_lik_i_upars()needs thegenerated quantitiesblock to computelog_lik, and the block also runs the*_rngcalls. Users can move the*_rngcode to a standalone generated quantities run.Tests
Four new tests in
tests/testthat/test_loo_moment_matching.Ruse the existing test model, with alog_lik_i_upars()orlog_lik_i()that fails in chosen calls:split = FALSE.log_lik_i()fails for observation 1, withk_threshold = 0so that 30 observations go through moment matching, once withcores = 1and once withcores = 2. Observation 1 keeps its PSIS results, and the other observations get the same results as without the error.With the master
R/loo_moment_matching.R, all four tests fail: three stop with an error, and one fails an expectation. With this PR,devtools::test()withNOT_CRAN=truepasses all 1280 tests and gives no warnings (R 4.6.1, testthat 3.3.2, Linux x86_64).